package com.easywan.web.controller.system;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.easywan.core.JSPModelAndView;
import com.easywan.service.ArticleService;
import com.easywan.service.IWanActiveService;
import com.easywan.service.SpotsService;
import com.easywan.service.UsersService;
import com.easywan.util.Utils;
import com.easywan.vo.Article;
import com.easywan.vo.Spots;
import com.easywan.vo.Users;
import com.easywan.vo.WanActive;

@Controller
public class ArticleController {
	private static Logger logger = Logger.getLogger(ArticleController.class);
	@Autowired
	private ArticleService articleService;
	@Autowired
	private SpotsService spotsService;
	@Autowired
	private UsersService usersService;
	@Autowired
	private IWanActiveService wanActiveService;
	@RequestMapping("/article/{id}.html")
	public ModelAndView article(@PathVariable("id") Integer id,ModelMap modelMap,HttpSession session){
		Article article = articleService.reads(id,Utils.getUserInfo(session));
		
		modelMap.addAttribute("article",article);
		//modelMap.addAttribute("articleList",articleService.selectLast(4));
		Users user = usersService.findById(article.getUid());
		modelMap.addAttribute("user",user);
		return new JSPModelAndView("system/article",modelMap);
	}
	@RequestMapping("article.html")
	public ModelAndView articleList(HttpServletRequest request,ModelMap modelMap){
		
		return new JSPModelAndView("system/article",modelMap);
	}
	@RequestMapping("/active/{id}.html")
	public ModelAndView active(@PathVariable("id") Integer id,ModelMap modelMap,HttpSession session){
		WanActive wan = wanActiveService.findById(id);
		modelMap.addAttribute("data",wan);
		wanActiveService.modify(wan.setLooks(wan.getLooks()+1));
		Users user = usersService.findById(1);
		modelMap.addAttribute("user",user);
		return new JSPModelAndView("system/spots",modelMap);
	}
	@RequestMapping("/spots/{id}.html")
	public ModelAndView spots(@PathVariable("id") Integer id,ModelMap modelMap,HttpSession session){
		Spots spots = spotsService.findById(id);
		modelMap.addAttribute("data",spots);
		spotsService.modify(spots.setLooks(spots.getLooks()+1));
		Users user = usersService.findById(spots.getUid());
		modelMap.addAttribute("user",user);
		return new JSPModelAndView("system/spots",modelMap);
	}
}
